{
    "cells": [
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "from notebook_utils import *\n",
                "\n",
                "b_ts, t_ts = 0, 0"
            ]
        },
        {
            "attachments": {},
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "# Temporal"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "cam_T_P = [[-4,0],[-3,0],[-2,0],[-1,0],[0,0]]\n",
                "bev_T_P = [[-4,0],[-3,0],[-2,0],[-1,0],[0,0]]\n",
                "Tout = len(bev_T_P)\n",
                "with initialize(version_base=\"1.3\", config_path=\"../configs/\"):\n",
                "    cfg = compose(\n",
                "        config_name=\"train.yaml\",\n",
                "        return_hydra_config=True,\n",
                "        overrides=[\n",
                "            f\"data.cam_T_P={cam_T_P}\",\n",
                "            f\"data.bev_T_P={bev_T_P}\",\n",
                "            \"data.version=mini\",\n",
                "            \"data.keep_input_binimg=True\",\n",
                "            \"data.keep_input_centr_offs=True\",\n",
                "            \"data.keep_input_detection=True\",\n",
                "            \"data.keep_input_persp=True\",\n",
                "            \"data.keep_input_sampling=True\",\n",
                "            \"data.coeffs.bev_aug.trans_rot=[50,50,0,50,0,0]\",\n",
                "            \"data.img_params.zoom_lim=[0.5,1.5]\",\n",
                "            \"data.img_params.rot_lim=[-45,45]\",\n",
                "            \"data.img_params.min_visibility=2\",\n",
                "        ],\n",
                "    )\n",
                "\n",
                "    cfg.paths.root_dir = str(\n",
                "        pyrootutils.find_root(search_from=\"../\", indicator=\".project-root\")\n",
                "    )"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "datamodule = hydra.utils.instantiate(cfg.data)\n",
                "datamodule.setup()\n",
                "dataloader = datamodule.train_dataloader()\n",
                "iter_dataloader = iter(dataloader)\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "data = next(iter_dataloader)\n",
                "data = datamodule.on_after_batch_transfer(data, 0)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "keys = list(data.keys())\n",
                "keys.sort()\n",
                "keys"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# data[\"egoTin_to_seq\"][b_ts]"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# data[\"bev_aug\"][b_ts]"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# data[\"egoTout_to_seq\"][b_ts]"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "px.imshow(\n",
                "    data[\"offsets\"][b_ts, :, 0],\n",
                "    facet_col=0,\n",
                "    facet_col_wrap=Tout,\n",
                "    binary_string=True,\n",
                "    width=600,\n",
                "    height=300,\n",
                ")"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "px.imshow(\n",
                "    data[\"binimg\"][b_ts, :, 0],\n",
                "    facet_col=0,\n",
                "    facet_col_wrap=Tout,\n",
                "    binary_string=True,\n",
                "    width=600,\n",
                "    height=300,\n",
                ")"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# Imgs\n",
                "grid = make_grid(\n",
                "    data[\"imgs\"][b_ts].flatten(0, 1),\n",
                "    normalize=True,\n",
                "    pad_value=1,\n",
                "    padding=3,\n",
                "    nrow=6,\n",
                ")\n",
                "show(grid)\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "from pointbev.utils.imgs import prepare_to_render_bbox_egopose\n",
                "\n",
                "px.imshow(\n",
                "    prepare_to_render_bbox_egopose(data[\"bbox_egopose\"][b_ts], True)\n",
                "    .transpose(0, 1)\n",
                "    .flip(0, 1),\n",
                "    binary_string=True,\n",
                ")\n"
            ]
        },
        {
            "attachments": {},
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "# Dataset"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "with initialize(version_base=\"1.3\", config_path=\"../configs/\"):\n",
                "    cfg = compose(\n",
                "        config_name=\"train.yaml\",\n",
                "        return_hydra_config=True,\n",
                "        overrides=[\n",
                "            \"data.version=mini\",\n",
                "            \"data.keep_input_binimg=True\",\n",
                "            \"data.keep_input_centr_offs=True\",\n",
                "            \"data.keep_input_detection=True\",\n",
                "            \"data.keep_input_persp=True\",\n",
                "            \"data.keep_input_sampling=True\",\n",
                "            \"data.keep_input_offsets_map=True\",\n",
                "            \"data.keep_input_lidar=True\",\n",
                "            \"data.kernel_scales=[1,2]\",\n",
                "            \"data.train_shuffle=True\",\n",
                "            \"data.coeffs.bev_aug.trans_rot=[20,20,0,20,0,0]\",\n",
                "            \"data.img_params.zoom_lim=[0.5,1.5]\",\n",
                "            \"data.img_params.rot_lim=[-45,45]\",\n",
                "            \"data.img_params.min_visibility=1\",\n",
                "        ],\n",
                "    )\n",
                "\n",
                "    cfg.paths.root_dir = str(\n",
                "        pyrootutils.find_root(search_from=\"../\", indicator=\".project-root\")\n",
                "    )\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "datamodule = hydra.utils.instantiate(cfg.data)\n",
                "datamodule.setup()\n",
                "datamodule.visualise_mode = True\n",
                "dataloader = datamodule.train_dataloader()\n",
                "iter_dataloader = iter(dataloader)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "data = next(iter_dataloader)\n",
                "data = datamodule.on_after_batch_transfer(data, 0)"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## Cameras"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# Imgs\n",
                "grid = make_grid(data[\"imgs\"][b_ts, t_ts], normalize=True, scale_each=True)\n",
                "show(grid)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# Imgs-Mask\n",
                "grid_mask = make_grid(\n",
                "    data[\"persp_imgs\"][b_ts, t_ts].unsqueeze(1).float(), normalize=True, scale_each=True\n",
                ")\n",
                "show(grid_mask)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# Image-Mask\n",
                "fig = go.Figure()\n",
                "alphabg = 0.3\n",
                "alphafg = 0.2\n",
                "blendarray = (\n",
                "    alphabg * grid.permute(1, 2, 0)\n",
                "    + alphafg * (1 - alphabg) * grid_mask.permute(1, 2, 0)\n",
                ") / (alphabg + alphafg * (1 - alphabg))\n",
                "px.imshow(blendarray)"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## BEV"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# Binimg and valid_binimg\n",
                "px.imshow(\n",
                "    torch.stack(\n",
                "        [\n",
                "            data[\"binimg\"][b_ts, t_ts].squeeze(0),\n",
                "            data[\"binimg_aug\"][b_ts, t_ts].squeeze(0),\n",
                "            data[\"lidar_img\"][b_ts, t_ts].squeeze(0),\n",
                "            data[\"lidar_img_aug\"][b_ts, t_ts].squeeze(0),\n",
                "            data[\"valid_binimg\"][b_ts, t_ts].squeeze(0),\n",
                "            data[\"valid_binimg_aug\"][b_ts, t_ts].squeeze(0),\n",
                "            ((1 - data[\"binimg_aug\"]) * data[\"valid_binimg_aug\"])[b_ts, t_ts].squeeze(\n",
                "                0\n",
                "            ),\n",
                "        ],\n",
                "        dim=0,\n",
                "    ),\n",
                "    facet_col=0,\n",
                "    facet_col_wrap=7,\n",
                "    binary_string=True,\n",
                "    width=1500,\n",
                ")"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# Mobility\n",
                "mobility = data[\"mobility\"][b_ts, t_ts].float()\n",
                "mobility_aug = data[\"mobility_aug\"][b_ts, t_ts].float()\n",
                "px.imshow(\n",
                "    torch.stack(\n",
                "        [\n",
                "            (mobility == 0),\n",
                "            (mobility == 1),\n",
                "            (mobility == 2),\n",
                "            (mobility == 3),\n",
                "            (mobility_aug == 0),\n",
                "            (mobility_aug == 1),\n",
                "            (mobility_aug == 2),\n",
                "            (mobility_aug == 3),\n",
                "        ],\n",
                "        dim=1,\n",
                "    ).squeeze(0),\n",
                "    facet_col=0,\n",
                "    facet_col_wrap=4,\n",
                ")"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# Offsets\n",
                "data[\"offsets\"][data[\"offsets\"]==255] = 0\n",
                "data[\"offsets_aug\"][data[\"offsets_aug\"]==255] = 0\n",
                "px.imshow(\n",
                "    torch.cat([data[\"offsets\"][b_ts, t_ts], data[\"offsets_aug\"][b_ts, t_ts]]),\n",
                "    facet_col=0,\n",
                "    facet_col_wrap=2,\n",
                "    width=600,\n",
                ")\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# Centerness\n",
                "px.imshow(\n",
                "    torch.cat(\n",
                "        [\n",
                "            data[\"centerness\"][b_ts, t_ts],\n",
                "            data[\"valid_centerness\"][b_ts, t_ts],\n",
                "            (data[\"valid_centerness\"] * data[\"centerness\"])[b_ts, t_ts],\n",
                "            data[\"centerness_aug\"][b_ts, t_ts],\n",
                "            data[\"valid_centerness_aug\"][b_ts, t_ts],\n",
                "            (data[\"valid_centerness_aug\"] * data[\"centerness_aug\"])[b_ts, t_ts],\n",
                "        ]\n",
                "    ).squeeze(0),\n",
                "    facet_col=0,\n",
                "    facet_col_wrap=3,\n",
                "    width=700,\n",
                ")\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# Offsets map\n",
                "px.imshow(\n",
                "    torch.cat(\n",
                "        [data[\"offsets_map\"][b_ts, t_ts], data[\"offsets_map_aug\"][b_ts, t_ts]]\n",
                "    ),\n",
                "    facet_col=0,\n",
                "    facet_col_wrap=2,\n",
                "    width=600,\n",
                ")\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# Offsets map distance\n",
                "px.imshow(\n",
                "    torch.cat(\n",
                "        [\n",
                "            (data[\"offsets_map_dist\"][b_ts, t_ts]).float(),\n",
                "            data[\"offsets_map_dist_aug\"][b_ts, t_ts],\n",
                "        ]\n",
                "    ),\n",
                "    facet_col=0,\n",
                "    facet_col_wrap=2,\n",
                "    width=600,\n",
                ")"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# HDMap\n",
                "if \"hdmap\" in data.keys():\n",
                "    px.imshow(\n",
                "        data[\"hdmap\"][b_ts, t_ts].permute(1, 2, 0)[..., 0]\n",
                "        + data[\"centerness\"][b_ts, t_ts, 0].round() * 2\n",
                "    )\n"
            ]
        }
    ],
    "metadata": {
        "kernelspec": {
            "display_name": "Python 3",
            "language": "python",
            "name": "python3"
        },
        "language_info": {
            "codemirror_mode": {
                "name": "ipython",
                "version": 3
            },
            "file_extension": ".py",
            "mimetype": "text/x-python",
            "name": "python",
            "nbconvert_exporter": "python",
            "pygments_lexer": "ipython3",
            "version": "3.11.8"
        },
        "orig_nbformat": 4
    },
    "nbformat": 4,
    "nbformat_minor": 2
}
